package nc.tb.bc.entryquery;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

import nc.tb.bc.table.ICellLinkableModel;

public class EntryQueryTableRender extends DefaultTableCellRenderer {

	Component render =  null;
	Color curBg = new Color(240, 240, 240);
	Color oldBg = null;
	
	public EntryQueryTableRender() {
		super();
		oldBg = this.getBackground();
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		render = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		TableModel model = table.getModel();
		if (model instanceof ICellLinkableModel && 
			((ICellLinkableModel)model).hasLink(row, column)) {
			render.setBackground(curBg);
		}else if(isSelected){
			render.setBackground(Color.orange);
		}else
			render.setBackground(oldBg);
		if (render instanceof JLabel) {
			if (model instanceof AbstractEntryQueryTableModel && 
				((AbstractEntryQueryTableModel)model).isDoubleCell(row, column)) {
				((JLabel)render).setHorizontalAlignment(SwingConstants.RIGHT);
			}
			else {
				((JLabel)render).setHorizontalAlignment(SwingConstants.LEFT);
			}
		}
		return render;
	}
}
